/* Set the defaults for DataTables initialisation */
$.extend( true, $.fn.dataTable.defaults, {
	"oLanguage": {
		"sEmptyTable": "没有找到数据！",
	},
	// http://datatables.net/reference/option/  默认参数    /* ThinkGem */
	"destroy": true,
	"scrollY": 150,
	"scrollX": false,
	"scrollCollapse": false,
	"autoWidth": false,
    "searching": false,
    "ordering": false,
	"paging": false,
	"info": false
});

// 数据表高度获取或刷新  /* ThinkGem */
$.fn.dataTable.autoHeight = function (dataTable, reDraw){
	// 计算和获取 DataTable 高度
	var dataTableHeight = $(dataTable).height() - 30;
	var dataTableBody = $(dataTable).parent('.dataTables_scrollBody');
	if (dataTableBody.length != 0){
		dataTableHeight = dataTableBody.height() - 10;
	}
	dataTableHeight = ($(window).height() - $('body').height() + dataTableHeight);
	if (dataTableHeight < 150){
		dataTableHeight = 150; // 最小高度
	}
	dataTableBody.height(dataTableHeight);
	if (reDraw){
		dataTable.api().draw();
	}
	return dataTableHeight;
}

// 绑定窗体大小变化时自动调整dataTable高度  /* ThinkGem */
$.fn.dataTable.bindAutoHeight = function (dataTable){
	$(window).resize(function(){
		$.fn.dataTable.autoHeight(dataTable, true);
	});
	if (Metronic.isIE8()){
		setTimeout(function(){
			$(window).resize();
		}, 1000);
	}
}

/**
 * datatable 扩展
 */
var DatatableExtend = function(settings){
	
	var $this = this;

	$this.dataTable;
	$this.dataTableLength = 0;
	
	$this.settings = $.extend({
		"winObj": 			$(window),				/* window对象 */
		"o_searchForm": 	$('#searchForm'),		/* 查询form */
		"o_dataTable": 		$('#dataTable'),		/* 数据table */
		"o_dataTableList": 	$('#dataTableList'),	/* 数据tbody */
		"o_serialNo":		$('#serialNo'),			/* 序号thead、th的serialNo */
		"columnDefs": 		[],						/* 不排序的列 */
		"o_page":	 		$('.pagination'),		/* 分页div的class */
		"showDataCallback":	 showData,				/* 显示数据的方法  */
		/** add param*/
		"options":			{}						/* datatable options*/
    }, settings);
	
	/** 移除dataTable中的数据信息，重新加载后重新设置 */
	$this.destroy = function(){
		if ($this.dataTable){
			$this.dataTable.api().destroy();
		}else{
			$this.dataTable = $this.settings.o_dataTable;
		}
	}
	
	/** 初始化dataTable */
	$this.init = function(list){
		
		$this.dataTableLength = list.length;
		
		// 排序时调用
		$this.dataTable.on('order.dt', function(e){
			$this.removeASC();
			if ($this.dataTableLength > 0){
				// 设置编号，排序时编号从1开始
				var serialNo = 1;
				$this.settings.o_dataTableList.children().each(function(){
					$(this).children().eq(0).html(serialNo);
					serialNo++;
				});
			}
		});
		// 初始化数据表
		if($this.settings.columnDefs.length==0){
			// 升级后不使用该参数
			var options = $this.settings.options;
			options.scrollY=$.fn.dataTable.autoHeight($this.settings.o_dataTable);
			$this.dataTable.dataTable(options);
		}else{
			$this.dataTable.dataTable({
				// http://datatables.net/reference/option/			
				"scrollY": $.fn.dataTable.autoHeight($this.settings.o_dataTable),
			    "ordering": true,
			    "columnDefs":  $this.settings.columnDefs // 不使用排序的列，从0开始
				//"order": [[ 1, "asc" ]]			
			});
		}
		$.fn.dataTable.bindAutoHeight($this.dataTable);// 自适应高度
		$this.settings.winObj.resize();
	}
	
	/** 设置分页信息 */
	$this.setPage = function(pageHtml){
		$this.settings.o_page.html(pageHtml);
	}
	
	/** 移除默认排序按钮样式 */
	$this.removeASC = function(){
		$this.settings.o_serialNo.removeClass('sorting_asc');
	}
	
	/** 绑定submit事件，页面有分页，查询时使用该方法执行ajax操作 */
	$this.bindSubmit = function(){
		$this.settings.o_searchForm.validate({
			submitHandler: function(form){
				ajaxSubmitForm($(form), function(renderResult){
					if(renderResult.result == Global.TRUE){
						var pageJson = JSON.parse(renderResult.message);
						
						// 显示数据并设置排序
						
						// 1、销毁
						$this.destroy();
						// 2、显示数据
						$this.settings.showDataCallback(pageJson.list);
						// 3、初始化
						$this.init(pageJson.list);
						
						// 显示分页信息
						$this.setPage(pageJson.html);
						
					}
				}, "json");
		    }
		});
	}
	
}

